package org.ouwh.oowiki;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class Util {

	public static boolean checkTime(String time) {
		String patterDay = "\\d{2}\\:\\d{2}";

		if (!Pattern.matches(patterDay, time)) {
			return false;
		}
		String hour = time.substring(0, 2);
		String min = time.substring(3, 5);
		int h = Integer.parseInt(hour);
		int m = Integer.parseInt(min);
		if (h >= 24 || m >= 60) {
			return false;
		}
		return true;
	}

	public static boolean isNumeric(String str) {
		String patter = "\\d{1,9}";
		if (!Pattern.matches(patter, str)) {
			return false;
		}
		return true;
	}

	public static String minusDate(String currentDate, int decrease) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date thisDate = format.parse(currentDate);
		long thisDay = thisDate.getTime();
		long resultDay = thisDay - 24 * 60 * 60 * 1000L * decrease;
		Date resultDate = new Date();
		resultDate.setTime(resultDay);
		String strResultDate = format.format(resultDate);
		return strResultDate;
	}

	public static boolean checkDate(String date) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Date day = format.parse(date);
			String date2 = format.format(day);
			if (date2.equals(date)) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

}
